package com.ipan.weixin.msg.general;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 音乐消息
 * 
 * @author iPan
 * @version 2013-12-24
 */
public class MusicMsg extends PlainMsg {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -4222238546556652114L;
	
	/** 音乐标题 */
	private String title;
	
	/** 音乐描述 */
	private String description;
	
	/** 音乐链接 */
	private String musicUrl;
	
	/** 高质量音乐链接，WIFI环境优先使用该链接播放音乐 */
	private String hqMusicUrl;
	
	/** 缩略图的媒体id，通过上传多媒体文件，得到的id */
	private String thumbMediaId;
	
	public MusicMsg() {
		super(TransportType.REPLY);
	}
	
	public MusicMsg(Document doc) {
		this();
		fromXml(doc);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getMusicUrl() {
		return musicUrl;
	}

	public void setMusicUrl(String musicUrl) {
		this.musicUrl = musicUrl;
	}

	public String getHqMusicUrl() {
		return hqMusicUrl;
	}

	public void setHqMusicUrl(String hqMusicUrl) {
		this.hqMusicUrl = hqMusicUrl;
	}

	public String getThumbMediaId() {
		return thumbMediaId;
	}

	public void setThumbMediaId(String thumbMediaId) {
		this.thumbMediaId = thumbMediaId;
	}
	
	@Override
	public void fromXml(Document doc) {
		if (doc == null) {
			return ;
		}
		
		super.fromXml(doc);
		Element root = doc.getRootElement();
		Element elMusic = root.element(EL_MUSIC);
		if (elMusic != null) {
			this.title = elMusic.elementTextTrim(EL_TITLE);
			this.description = elMusic.elementTextTrim(EL_DESCRIPTION);
			this.musicUrl = elMusic.elementTextTrim(EL_MUSIC_URL);
			this.hqMusicUrl = elMusic.elementTextTrim(EL_HQ_MUSIC_URL);
			this.thumbMediaId = elMusic.elementTextTrim(EL_THUMB_MEDIA_ID);
		}
	}
	
	@Override
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder("<xml>");
		buf.append(super.toXml(nonEmpty));
		buf.append(wrapElementHead(EL_MSG_TYPE)).append(TYPE_MUSIC).append(wrapElementEnd(EL_MSG_TYPE));
		if (nonEmpty) {
			buf.append(wrapElementHead(EL_MUSIC));
			if (StringUtils.isNotEmpty(this.title)) {
				buf.append(wrapElementHead(EL_TITLE)).append(this.title).append(wrapElementEnd(EL_TITLE));
			}
			if (StringUtils.isNotEmpty(this.description)) {
				buf.append(wrapElementHead(EL_DESCRIPTION)).append(this.description).append(wrapElementEnd(EL_DESCRIPTION));
			}
			if (StringUtils.isNotEmpty(this.musicUrl)) {
				buf.append(wrapElementHead(EL_MUSIC_URL)).append(this.musicUrl).append(wrapElementEnd(EL_MUSIC_URL));
			}
			if (StringUtils.isNotEmpty(this.hqMusicUrl)) {
				buf.append(wrapElementHead(EL_HQ_MUSIC_URL)).append(getStringValue(this.hqMusicUrl)).append(wrapElementEnd(EL_HQ_MUSIC_URL));
			}
			if (StringUtils.isNotEmpty(this.thumbMediaId)) {
				buf.append(wrapElementHead(EL_THUMB_MEDIA_ID)).append(getStringValue(this.thumbMediaId)).append(wrapElementEnd(EL_THUMB_MEDIA_ID));
			}
			buf.append(wrapElementEnd(EL_MUSIC));
		} else {
			buf.append(wrapElementHead(EL_MUSIC));
			buf.append(wrapElementHead(EL_TITLE)).append(getStringValue(this.title)).append(wrapElementEnd(EL_TITLE));
			buf.append(wrapElementHead(EL_DESCRIPTION)).append(getStringValue(this.description)).append(wrapElementEnd(EL_DESCRIPTION));
			buf.append(wrapElementHead(EL_MUSIC_URL)).append(getStringValue(this.musicUrl)).append(wrapElementEnd(EL_MUSIC_URL));
			buf.append(wrapElementHead(EL_HQ_MUSIC_URL)).append(getStringValue(this.hqMusicUrl)).append(wrapElementEnd(EL_HQ_MUSIC_URL));
			buf.append(wrapElementHead(EL_THUMB_MEDIA_ID)).append(getStringValue(this.thumbMediaId)).append(wrapElementEnd(EL_THUMB_MEDIA_ID));
			buf.append(wrapElementEnd(EL_MUSIC));
		}
		return buf.append("</xml>").toString();
	}

}

/*
回复：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[TITLE]]></Title>
<Description><![CDATA[DESCRIPTION]]></Description>
<MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl>
<HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl>
<ThumbMediaId><![CDATA[media_id]]></ThumbMediaId>
</Music>
</xml>
*/
